package com.heking.zplayer.qlyopen;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;


import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-11-05
 * @Description:
 */
public abstract class BaseQlyPlayer implements IQlyPlayer {
    private static final String TAG = ">>>BaseQlyPlayer";
    protected Context mContext;

    protected ViewGroup mPlayViewGroup;
    protected String  mDeviceId;
    protected boolean mMute=true;//默认静音
    protected @IQlyPlayer.State int mPlayState = STATE_IDLE;//播放状态
    protected PlayListener mPlayListener;
    protected OnTalkListener mOnTalkListener;
    protected CapturePictureListener mCapturePictureListener;
    protected Callback<QlyCameraAbility> mOnCameraAbilityChange;
    protected Callback<QlyCameraSetting> mOnQlyCameraSettingChange;

    public BaseQlyPlayer(Context context) {
        this.mContext = context;
    }

    /**
     * 设置PlayView容器
     *
     * @param viewGroup
     */
    @Override
    public void setPlayViewGroup(@Nullable ViewGroup viewGroup) {
        mPlayViewGroup = viewGroup;
    }

    /**
     * 设置播放url
     *
     * @param url
     */
    @Override
    public void setPlayUrl(@Nullable String url){
        Log.d(TAG, "setPlayUrl:"+url);
        this.mDeviceId=url;
    }

    /**
     * 设置静音
     *
     * @param mute true:静音,false:非静音
     */
    @Override
    public void setMute(boolean mute) {
        mMute=mute;
    }

    /**
     * 是否是静音
     *
     * @return
     */
    @Override
    public boolean isMute() {
        return mMute;
    }

    /**
     * 是否正在播放
     *
     * @return
     */
    @Override
    public boolean isPlaying() {
        return mPlayState==IQlyPlayer.STATE_PLAYING;
    }

    /**
     * 获取播放状态
     *
     * @return
     */
    @Override
    public int getPlaySate() {
        return mPlayState;
    }

    /**
     * 播放监听
     *
     * @param playListener
     */
    @Override
    public void setPlayListener(@Nullable PlayListener playListener) {
        mPlayListener=playListener;
    }

    @Override
    public void setOnTalkListener(@Nullable OnTalkListener onTalkListener) {
        mOnTalkListener=onTalkListener;
    }

    @Override
    public void setOnCameraAbilityChange(@Nullable Callback<QlyCameraAbility> callback) {
        mOnCameraAbilityChange=callback;
    }

    @Override
    public void setOnQlyCameraSettingChange(@Nullable Callback<QlyCameraSetting> callback) {
        mOnQlyCameraSettingChange=callback;
    }
    /**
     * 设置截屏回掉
     *
     * @param capturePictureListener
     */
    @Override
    public  void setCapturePictureListener(@Nullable CapturePictureListener capturePictureListener){
        this.mCapturePictureListener=capturePictureListener;
    }
    /**
     * 播放状态改变时调用
     *
     * @param status
     */
    protected void callStatusChange(@State int status) {
        mPlayState = status;
//        Log.d(TAG, "invokeStatusChange:" + Thread.currentThread().getName());
        if (mPlayListener != null) {
            ThreadUtils.runMainThread(() -> mPlayListener.onPlayStatusChange(status));
        }
    }

    /**
     * 播放错误时调用
     *
     * @param error
     */
    protected void callError(@PlayError.ErrorCode int code, String error) {
//        Log.e(TAG, "playError:" + error);
        if (mPlayListener != null) {
            ThreadUtils.runMainThread(() -> mPlayListener.onPlayError(code, error));
        }
    }

    /**
     * 播放状态改变时调用
     *
     * @param status
     */
    protected void callTalking(@OnTalkListener.TalkStatus int status,String error) {
        if (mOnTalkListener != null) {
            ThreadUtils.runMainThread(() -> mOnTalkListener.onTalkCallback(status,error));
        }
    }

}
